package view;

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import parser.Parser;
import socialnetwork.SocialNetwork;
import view.paper.PaperPanel;
import view.researcher.ResearcherPanel;
import db.DAOFacade;
import db.Database;
import dto.Paper;
import dto.Researcher;

public class MainAplication {

	private JFrame frame;
	public static MainAplication window = new MainAplication();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Database.getConnection();
					Database.createDataBase();
					SocialNetwork.addResearchers(new DAOFacade().selectResearchers());
					Database.disconnect();
					window = new MainAplication();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainAplication() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle("Red Social CONICET");
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu mnAgregarElemento = new JMenu("Agregar Elemento");
		menuBar.add(mnAgregarElemento);
		
		JMenuItem mntmInvestigador = new JMenuItem("Investigador");
		mntmInvestigador.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String input = JOptionPane.showInputDialog(frame, "ID's separadas por ';'", "Agregar Investigadores", JOptionPane.PLAIN_MESSAGE);
				if (input != null){
					String[] ids = input.split(";");
					for (String s : ids){
						Researcher r = new Parser().parseForResearcher(Integer.valueOf(s));
						SocialNetwork.addResearcher(r);
					}
				}
			}
		});
		mnAgregarElemento.add(mntmInvestigador);
		
		JMenu mnTest = new JMenu("Navegar");
		menuBar.add(mnTest);
		
		JMenuItem mntmInicio = new JMenuItem("Inicio");
		mntmInicio.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showResearcherList();
			}
		});
		
		mnTest.add(mntmInicio);
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		frame.getContentPane().setLayout(gridBagLayout);
		
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		frame.getContentPane().add(panel, gbc_panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{0, 0};
		gbl_panel.rowHeights = new int[]{0, 0};
		gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		
		
		GridBagLayout gridBagLayout_1 = (GridBagLayout) researcherListPanel.getLayout();
		gridBagLayout_1.rowWeights = new double[]{1.0};
		gridBagLayout_1.rowHeights = new int[]{0};
		gridBagLayout_1.columnWeights = new double[]{1.0};
		gridBagLayout_1.columnWidths = new int[]{0};
		GridBagConstraints gbc_researcherListPanel = new GridBagConstraints();
		gbc_researcherListPanel.fill = GridBagConstraints.BOTH;
		gbc_researcherListPanel.gridx = 0;
		gbc_researcherListPanel.gridy = 0;
		panel.add(researcherListPanel, gbc_researcherListPanel);
	};
	private PaperPanel paperPanel = new PaperPanel();
	private ResearcherPanel researcherPanel = new ResearcherPanel();
	private ResearcherListPanel researcherListPanel = new ResearcherListPanel();
	private JPanel panel = new JPanel();
	
	public void showResearcherList() {
		paperPanel.setVisible(false);
		researcherPanel.setVisible(false);
		researcherListPanel.setVisible(true);
		GridBagConstraints gbc_researcherListPanel = new GridBagConstraints();
		gbc_researcherListPanel.fill = GridBagConstraints.BOTH;
		gbc_researcherListPanel.gridx = 0;
		gbc_researcherListPanel.gridy = 0;
		panel.add(researcherListPanel, gbc_researcherListPanel);
	}

	public void showPaper(Paper p) {
		if (p != null && JOptionPane.showConfirmDialog(frame, "Desea Visualizar el articulo clickeado? ", "Visualizar Articulo", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION){
			paperPanel.setPaper(p);
			
			researcherListPanel.setVisible(false);
			researcherPanel.setVisible(false);
			paperPanel.setVisible(true);
			GridBagConstraints gbc_researcherListPanel = new GridBagConstraints();
			gbc_researcherListPanel.fill = GridBagConstraints.BOTH;
			gbc_researcherListPanel.gridx = 0;
			gbc_researcherListPanel.gridy = 0;
			panel.add(paperPanel, gbc_researcherListPanel);
		}
	}

	public void showResearcher(Researcher r, boolean showPapers) {
		String msj = null;
		if (showPapers){
			msj = "articulos";
		} else {
			msj = "datos";
		}
		if (r != null && JOptionPane.showConfirmDialog(frame, "Desea Visualizar los " + msj + " del investigador " + r.getName() + "?", "Visualizar Investigador", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION){
			researcherPanel.setResearcher(r, showPapers);
						
			paperPanel.setVisible(false);
			researcherListPanel.setVisible(false);
			researcherPanel.setVisible(true);
			GridBagConstraints gbc_researcherListPanel = new GridBagConstraints();
			gbc_researcherListPanel.fill = GridBagConstraints.BOTH;
			gbc_researcherListPanel.gridx = 0;
			gbc_researcherListPanel.gridy = 0;
			panel.add(researcherPanel, gbc_researcherListPanel);
		}
	}

	public void showResearcher() {
		researcherPanel.setView(false);
		
		paperPanel.setVisible(false);
		researcherListPanel.setVisible(false);
		researcherPanel.setVisible(true);
		GridBagConstraints gbc_researcherListPanel = new GridBagConstraints();
		gbc_researcherListPanel.fill = GridBagConstraints.BOTH;
		gbc_researcherListPanel.gridx = 0;
		gbc_researcherListPanel.gridy = 0;
		panel.add(researcherPanel, gbc_researcherListPanel);
	}
}